//
//  VoteItemDetailViewController.m
//  homeland
//
//  Created by gzbugu on 14-8-2.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//  我要投票选项详情展示类
//

#import "VoteItemDetailViewController.h"
#import "UIImageView+WebCache.h"

@interface VoteItemDetailViewController ()

@end

@implementation VoteItemDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"我要投票";
    [self configView];
}

-(void)configView{
    NSDictionary *item = [_config objectForKey:@"dataItem"];
    //展示图片
    //--------------------
    NSString *pic = [NSString stringWithFormat:@"http://%@%@", SERVER_URL_COMMUNITY_VOTE_IMAGE,[CommonUtil dealNullObject:[item objectForKey:@"voteItemImg"]]];
    __block UIActivityIndicatorView *activityIndicator;
    __weak UIImageView *weakImageView = self.imageView;
    [self.imageView setImageWithURL:[NSURL URLWithString:pic] placeholderImage:[UIImage imageNamed:@"placeholder.png"] options:SDWebImageProgressiveDownload progress:^(NSUInteger receivedSize, long long expectedSize){
        if (!activityIndicator){
            [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
            activityIndicator.center = weakImageView.center;
            [activityIndicator startAnimating];
        }
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType){
        [activityIndicator removeFromSuperview];
        activityIndicator = nil;
    }];
    
    self.nameLabel.text = [CommonUtil dealNullObject:[item objectForKey:@"voteItemContext"]];
    self.nationLabel.text = [CommonUtil dealNullObject:[item objectForKey:@"nation"]];
    self.nationalityLabel.text = [CommonUtil dealNullObject:[item objectForKey:@"nationality"]];
    self.educationLabel.text = [CommonUtil dealNullObject:[item objectForKey:@"education"]];
    self.ageLabel.text = [CommonUtil dealNullObject:[item objectForKey:@"age"]];
    NSString *_Voccupation = [CommonUtil dealNullObject:[item objectForKey:@"occupation"]];
    //根据内容调整高度
    //300为UILabel的宽度，100是预设的一个高度，表示在这个范围内
    CGSize labelSize = [_Voccupation sizeWithFont:[UIFont systemFontOfSize:14]
                            constrainedToSize:CGSizeMake(110.0, 34)
                                lineBreakMode:NSLineBreakByWordWrapping];
    self.occupationLabel.font = [UIFont systemFontOfSize:14];//字体大小
    self.occupationLabel.textAlignment = NSTextAlignmentLeft;//文字居左显示
    self.occupationLabel.numberOfLines = 0;//表示label可以多行显示
    self.occupationLabel.lineBreakMode = NSLineBreakByWordWrapping;//换行模式，与上面的计算保持一致。
    self.occupationLabel.text = _Voccupation;
    self.occupationLabel.frame = CGRectMake(self.occupationLabel.frame.origin.x, self.occupationLabel.frame.origin.y,self.occupationLabel.frame.size.width, labelSize.height);
    //展示主体内容
    //-------------------
    
    //间隔线
    self.spiritLine.frame = CGRectMake(self.spiritLine.frame.origin.x,self.spiritLine.frame.origin.y,self.spiritLine.frame.size.width, 0.5);
    
    NSString *_Vcontent = [CommonUtil dealNullObject:[item objectForKey:@"personalProfile"]];
    //根据内容调整高度
    CGFloat contentHeight = [self caculateHeightForString:_Vcontent AndFontSize:14.0f AndWitdh:300.0f];
    self.contentTextView.frame = CGRectMake(self.contentTextView.frame.origin.x,self.contentTextView.frame.origin.y,self.contentTextView.frame.size.width, contentHeight);
    self.contentTextView.text = _Vcontent;
    
    CGFloat totalHeight = MAX(CGRectGetMaxY(self.contentTextView.frame)+10.0, CGRectGetHeight(self.scrollView.frame));
    self.mainPanel.frame = CGRectMake(0.0, 0.0, 320.0, totalHeight);
    [self.scrollView addSubview:self.mainPanel];
    self.scrollView.contentSize = self.mainPanel.frame.size;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

